package io.bsly.modules.job.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.bsly.common.utils.Constant;
import io.bsly.modules.app.entity.localEntity.DyCouponEntity;
import io.bsly.modules.app.entity.localEntity.DyOrderEntity;
import io.bsly.modules.app.service.localmaster.DyCouponService;
import io.bsly.modules.app.service.localmaster.DyOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component("orderStatusTask")
public class OrderStatusTask implements ITask{
    @Autowired
    private DyOrderService dyOrderService;

    @Autowired
    DyCouponService dyCouponService;

    @Override
    public void run(String params) {
        log.info("-=-=- OrderStatusTask 开始运行-=-=-"+new Date().toLocaleString());
        try {
            //获取订单状态为激活的订单;
            List<DyOrderEntity> listOrders = dyOrderService.list(new QueryWrapper<DyOrderEntity>().lambda().eq(DyOrderEntity::getStatus, Constant.OrderStatus.CONFIRMED.getValue()));
            listOrders.parallelStream().forEach(one ->{
                one.setStatus(Constant.OrderStatus.DONE.getValue());
            });
            dyOrderService.updateBatchById(listOrders);

            //更新预定信息的状态
            List<DyCouponEntity> listBooks = dyCouponService.list(new QueryWrapper<DyCouponEntity>().lambda()
                    .eq(DyCouponEntity::getStatus, Constant.CouponStatus.ACTIVE.getValue())
                    .eq(DyCouponEntity::getNightCount, 0));//无可用间夜数的预定订单
            listBooks.parallelStream().forEach(one ->{
                one.setStatus(Constant.CouponStatus.INVALID.getValue());
            });
            dyCouponService.updateBatchById(listBooks);
        }catch (Exception e){
           log.error("订单定时更新失败：",e);
        }


    }
}
